package com.cinc.ebc.sd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.mm.entity.MmStockInOut;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.service.IMmStockInOutService;
import com.cinc.ebc.sd.constant.SdConstant;
import com.cinc.ebc.sd.entity.SdDispatchLoad;
import com.cinc.ebc.sd.entity.SdSalesReturnDetail;
import com.cinc.ebc.sd.mapper.SdSalesReturnDetailMapper;
import com.cinc.ebc.sd.mapper.SdSalesReturnMapper;
import com.cinc.ebc.sd.model.SdSalesReturnDetailModel;
import com.cinc.ebc.sd.service.ISdSalesReturnDetailService;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 销售退货明细
 * @Author: jeecg-boot
 * @Date:   2022-10-09
 * @Version: V1.0
 */
@Service
public class SdSalesReturnDetailServiceImpl extends ServiceImpl<SdSalesReturnDetailMapper, SdSalesReturnDetail> implements ISdSalesReturnDetailService {

	@Autowired
	private SdSalesReturnDetailMapper sdSalesReturnDetailMapper;

	@Autowired
	private SdSalesReturnMapper sdSalesReturnMapper;

	@Autowired
	private IMmStockInOutService mmStockInOutService;

	@Override
	public List<SdSalesReturnDetail> selectByMainId(String mainId) {
		return sdSalesReturnDetailMapper.selectByMainId(mainId);
	}

	@Override
	public IPage<SdSalesReturnDetailModel> detailPage(Page<SdSalesReturnDetailModel> page, SdSalesReturnDetailModel detailModel) {
		return page.setRecords(sdSalesReturnDetailMapper.selectDetailList(page,detailModel));
	}

	@Transactional
	@Override
	public void handleConfirm(List<SdSalesReturnDetailModel> detailModelList) throws Exception {
		List<String> mainIdList = new ArrayList<>();
		List<String> customerIdList = detailModelList.stream().map(SdSalesReturnDetailModel::getCustomerId).distinct().collect(Collectors.toList());
		for(String customerId : customerIdList){
			List<MmStockInOutDetail> stockInOutDetailList = new ArrayList<>();
			int stockInOutDetaiLineNo = 0;
			String projectId = null;
			for(SdSalesReturnDetailModel detailModel : detailModelList){
				if(!customerId.equals(detailModel.getCustomerId())){
					continue;
				}
				SdSalesReturnDetail sdSalesReturnDetail = sdSalesReturnDetailMapper.selectById(detailModel.getId());
				if(!SdConstant.SAL_RETURNDETAIL_STATUS01.equals(sdSalesReturnDetail.getStatus())){
					throw new RuntimeException("该状态不能退货");
				}
				if(!mainIdList.contains(sdSalesReturnDetail.getMainId())){
					mainIdList.add(sdSalesReturnDetail.getMainId());
				}
				// 更新退货明细状态
				SdSalesReturnDetail returnDetailModify = new SdSalesReturnDetail();
				returnDetailModify.setId(detailModel.getId());
				returnDetailModify.setStatus(SdConstant.SAL_RETURNDETAIL_STATUS02);
				sdSalesReturnDetailMapper.updateById(returnDetailModify);
				projectId = detailModel.getProjectId();
				// 出库单明细
				MmStockInOutDetail stockInOutDetail = new MmStockInOutDetail();
				stockInOutDetaiLineNo += 10;
				stockInOutDetail.setLineNo(stockInOutDetaiLineNo);
				stockInOutDetail.setMaterialId(sdSalesReturnDetail.getMaterialId());
				stockInOutDetail.setMaterialNo(sdSalesReturnDetail.getMaterialNo());
				stockInOutDetail.setMaterialDesc(sdSalesReturnDetail.getMaterialDesc());
				stockInOutDetail.setUnit(sdSalesReturnDetail.getUnit());
				stockInOutDetail.setQty(sdSalesReturnDetail.getQty().negate());
				stockInOutDetail.setUnitPrice(sdSalesReturnDetail.getUnitPrice());
				BigDecimal sumPrice = sdSalesReturnDetail.getUnitPrice().multiply(stockInOutDetail.getQty()).setScale(2, BigDecimal.ROUND_HALF_UP);
				stockInOutDetail.setSumPrice(sumPrice);
				stockInOutDetail.setSourceId(sdSalesReturnDetail.getId());
				stockInOutDetail.setSourceNo(detailModel.getReturnNo());
				stockInOutDetail.setWarehouseId(detailModel.getWarehouseId());
				stockInOutDetail.setStorageLocationId(detailModel.getStorageLocationId());
				stockInOutDetail.setBatchNo(detailModel.getBatchNo());
				stockInOutDetail.setProjectId(projectId);
				stockInOutDetailList.add(stockInOutDetail);
			}
			// 生成出库单
			if(oConvertUtils.listIsNotEmpty(stockInOutDetailList)){
				MmStockInOut stockInOut = new MmStockInOut();
				stockInOut.setBillDate(new Date());
				stockInOut.setCustomerId(customerId);
				stockInOut.setProjectId(projectId);
				mmStockInOutService.saveMainSalesReturn(stockInOut, stockInOutDetailList);
			}
		}
		// 更新主表状态
		for(String id : mainIdList){
			sdSalesReturnMapper.autoUpdateStatus(id);
		}
	}
}
